home *** CD-ROM | disk | FTP | other *** search
/ Gamers Delight 2 / Gamers Delight 2.iso / Aminet / game / misc / Bomber.lha / Bomber / Sources / highscore.c < prev    next >
C/C++ Source or Header  |  1991-11-21  |  6KB  |  308 lines

  1. /*
  2.  *     highscore.c
  3.  *
  4.  */
  5.  
  6. #include "Bomber.h"
  7. #include "sound.h"
  8.  
  9.  
  10. #if DEBUG
  11. # define D(debug) debug
  12. #else
  13. # define D(debug)
  14. #endif
  15.  
  16.  
  17. /*------------------------- Externe Daten...*/
  18.  
  19.  
  20. extern struct ExecBase    *SysBase;
  21. extern struct Library    *DOSBase;
  22.  
  23. extern struct IntuitionBase    *IntuitionBase;
  24. extern struct GfxBase    *GfxBase;
  25. extern struct Library    *GadToolsBase;
  26.  
  27. extern struct Window    *window;
  28. extern struct Screen    *screen;
  29. extern void                *vi;
  30.  
  31. extern char                *texte_level[];
  32.  
  33. extern int                gesamtzeit;
  34.  
  35.  
  36. /*------------------------- Daten dieses Moduls...*/
  37.  
  38.  
  39. struct hisc_entry
  40. {
  41.     char    name[20];
  42.     int        size_x,
  43.             size_y,
  44.             level,
  45.             restzeit;
  46.     long    score;
  47. }                        hisc_liste[10];
  48.  
  49.  
  50. #define NAME(n)            hisc_liste[n].name
  51. #define X(n)            hisc_liste[n].size_x
  52. #define Y(n)            hisc_liste[n].size_y
  53. #define NLEVEL(n)        hisc_liste[n].level
  54. #define LEVEL(n)        texte_level[ NLEVEL(n) ]
  55. #define ZEIT(n)            hisc_liste[n].restzeit
  56. #define SCORE(n)        hisc_liste[n].score
  57.  
  58.  
  59. /*------------------------- Code-Deklarationen...*/
  60.  
  61.  
  62. extern void sound_play( int );
  63.  
  64.  
  65. /*------------------------- Stub-Code...*/
  66.  
  67.  
  68. LONG EasyRequest( struct Window *window, struct EasyStruct *easyStruct,
  69.     ULONG *idcmpPtr, APTR arg1, ... )
  70. {
  71.     return EasyRequestArgs( window, easyStruct, idcmpPtr, (struct TagItem *)&arg1 );
  72. }
  73.  
  74.  
  75. /*------------------------- Code-Definitionen...*/
  76.  
  77.  
  78. void load_hisc( void )
  79. {
  80.     BPTR fh;
  81.     
  82.     if( fh = Open( "Bomber.HiSc", MODE_OLDFILE ) )
  83.     {
  84.         Read( fh, hisc_liste, sizeof(hisc_liste) );
  85.         Close( fh );
  86.     }
  87.     else
  88.     {
  89.         memset( hisc_liste, 0, sizeof(hisc_liste) );
  90.     }
  91. }
  92.  
  93.  
  94. void save_hisc( void )
  95. {
  96.     BPTR fh;
  97.     
  98.     if( fh = Open( "Bomber.HiSc", MODE_NEWFILE ) )
  99.     {
  100.         Write( fh, hisc_liste, sizeof(hisc_liste) );
  101.         Close( fh );
  102.     }
  103. }
  104.  
  105.  
  106. void init_highscore( void )
  107. {
  108.     load_hisc();
  109. }
  110.  
  111.  
  112. void close_highscore( void )
  113. {
  114.     // Da der HiSc sicherheitshalber nach jedem Eintrag
  115.     // gespeichert wird, bleibt hier nix zu tun.
  116. }
  117.  
  118.  
  119. void highscore_show( void )
  120. {
  121.     static struct EasyStruct hiscreq = {
  122.         sizeof (struct EasyStruct), 0,
  123.         "Hall of Fame",
  124.         " 1. %-19s (%ld*%ld %s, %ld seconds left)\n"
  125.         " 2. %-19s (%ld*%ld %s, %ld seconds left)\n"
  126.         " 3. %-19s (%ld*%ld %s, %ld seconds left)\n"
  127.         " 4. %-19s (%ld*%ld %s, %ld seconds left)\n"
  128.         " 5. %-19s (%ld*%ld %s, %ld seconds left)\n"
  129.         " 6. %-19s (%ld*%ld %s, %ld seconds left)\n"
  130.         " 7. %-19s (%ld*%ld %s, %ld seconds left)\n"
  131.         " 8. %-19s (%ld*%ld %s, %ld seconds left)\n"
  132.         " 9. %-19s (%ld*%ld %s, %ld seconds left)\n"
  133.         "10. %-19s (%ld*%ld %s, %ld seconds left)",
  134.         " Back " };
  135.     
  136.     EasyRequest( window, &hiscreq, NULL,
  137.         NAME(0), X(0), Y(0), LEVEL(0), ZEIT(0),
  138.         NAME(1), X(1), Y(1), LEVEL(1), ZEIT(1),
  139.         NAME(2), X(2), Y(2), LEVEL(2), ZEIT(2),
  140.         NAME(3), X(3), Y(3), LEVEL(3), ZEIT(3),
  141.         NAME(4), X(4), Y(4), LEVEL(4), ZEIT(4),
  142.         NAME(5), X(5), Y(5), LEVEL(5), ZEIT(5),
  143.         NAME(6), X(6), Y(6), LEVEL(6), ZEIT(6),
  144.         NAME(7), X(7), Y(7), LEVEL(7), ZEIT(7),
  145.         NAME(8), X(8), Y(8), LEVEL(8), ZEIT(8),
  146.         NAME(9), X(9), Y(9), LEVEL(9), ZEIT(9) );
  147. }
  148.  
  149.  
  150. get_spielernamen( char *ziel )
  151. {
  152.     static char            input[20] = "";
  153.     
  154.     struct Window        *w;
  155.     struct NewGadget    ng;
  156.     struct Gadget        *glist=NULL, *gad;
  157.     int                    topborder,
  158.                         leftborder,
  159.                         width, height;
  160.     long                tl;
  161.     
  162.     struct IntuiMessage    *imsg;
  163.     ULONG                class;
  164.     BOOL                ende = FALSE;
  165.     
  166.     
  167.     // Create String Gadget
  168.     
  169.     topborder  = screen->WBorTop + screen->Font->ta_YSize + 1;
  170.     leftborder = screen->WBorLeft;
  171.     gad = CreateContext( & glist );
  172.     
  173.     ng.ng_TopEdge  = topborder + 3*INTERHEIGHT + screen->Font->ta_YSize;
  174.     ng.ng_LeftEdge = leftborder + INTERWIDTH;
  175.     tl = TextLength( &screen->RastPort, "XXXXXXXXXXXXXXXXXXXXXX", 22 );
  176.     ng.ng_Width = tl;
  177.     ng.ng_Height = screen->Font->ta_YSize + 4;
  178.     if( ng.ng_Height < 12 ) ng.ng_Height=12;
  179.     
  180.     ng.ng_VisualInfo = vi;
  181.     ng.ng_TextAttr = screen->Font;
  182.     ng.ng_GadgetText = "Please enter your Name:";
  183.     ng.ng_Flags = PLACETEXT_ABOVE;
  184.     
  185.     gad = CreateGadget( STRING_KIND, gad, &ng,
  186.         GTST_String, input,
  187.         GTST_MaxChars, 19,
  188.         TAG_DONE );
  189.     
  190.     if( !gad ) return;
  191.     
  192.     // Open Window
  193.     
  194.     width  = gad->LeftEdge + gad->Width + leftborder + 2*INTERWIDTH;
  195.     height = gad->TopEdge + gad->Height + 2*INTERHEIGHT;
  196.     
  197.     w = OpenWindowTags( NULL,
  198.         WA_Title,            "Congratulations!",
  199.         WA_Left,            (screen->Width  - width ) / 2,
  200.         WA_Top,                (screen->Height - height) / 2,
  201.         WA_Width,            width,
  202.         WA_Height,            height,
  203.         WA_PubScreen,        screen,
  204.         
  205.         WA_DragBar,            TRUE,
  206.         WA_DepthGadget,        TRUE,
  207.         WA_RMBTrap,            TRUE,
  208.         WA_SmartRefresh,    TRUE,
  209.         WA_Activate,        TRUE,
  210.         
  211.         WA_IDCMP, REFRESHWINDOW | STRINGIDCMP,
  212.         TAG_DONE );
  213.     
  214.     if( !w ) return;
  215.     
  216.     // Add Gadget
  217.     
  218.     AddGList( w, glist, 0, -1, NULL );
  219.     RefreshGList( glist, w, NULL, -1 );
  220.     GT_RefreshWindow( w, NULL );
  221.     
  222.     // Aktivieren
  223.     
  224.     ActivateGadget( gad, w, 0 );
  225.     
  226.     // MainLoop
  227.     
  228.     do
  229.     {
  230.         WaitPort( w->UserPort );
  231.         
  232.         while( imsg = GT_GetIMsg(w->UserPort) )
  233.         {
  234.             class = imsg->Class;
  235.             GT_ReplyIMsg( imsg );
  236.             
  237.             if( class == REFRESHWINDOW )
  238.             {
  239.                 GT_BeginRefresh( w );
  240.                 GT_EndRefresh( w, TRUE );
  241.             }
  242.             else
  243.             {
  244.                 ende = TRUE;
  245.             }
  246.         }
  247.     } while( !ende );
  248.     
  249.     // Eine Kopie hierbehalten
  250.     strcpy( input, ((struct StringInfo *)gad->SpecialInfo)->Buffer );
  251.     
  252.     // Resultat einsetzen
  253.     strcpy( ziel, input );
  254.     
  255.     // Schulz jetz
  256.     CloseWindow( w );
  257.     FreeGadgets( glist );
  258. }
  259.  
  260.  
  261. void highscore_eintrag( int x, int y, int zeit, int level )
  262. {
  263.     long    spieler_score;
  264.     int        i;
  265.     
  266.     D(printf("eintrag %d %d %d %d\n", x,y,zeit,level));
  267.     
  268.     /*
  269.      * SCORE-BERECHNUNG: Level sollte am stärksten eingehen,
  270.      * dann Feldgröße und zuletzt die Restzeit.
  271.      * (wobei die Feldgröße schon indirekt durch die Zeit drin ist)
  272.      * 
  273.      * Feldgröße: x*y = > 30 und < 50000
  274.      *
  275.      */
  276.     
  277.     spieler_score =   1000000 * level
  278.                     +      10 * x * y
  279.                     +     100 * zeit / gesamtzeit;
  280.     
  281.     for( i=0; i<=9; ++i )
  282.     {
  283.         if( spieler_score > hisc_liste[i].score )
  284.             break;
  285.     }
  286.     
  287.     if( i < 10 )
  288.     {
  289.         sound_play( SOUND_HIGHSCORE );
  290.         
  291.         if( i < 9 )
  292.         {
  293.             memmove( &hisc_liste[i+1], &hisc_liste[i],
  294.                 (9 - i) * sizeof(struct hisc_entry) );
  295.         }
  296.         
  297.         get_spielernamen( NAME(i) );
  298.         X(i)      = x;
  299.         Y(i)      = y;
  300.         NLEVEL(i) = level;
  301.         ZEIT(i)   = zeit;
  302.         SCORE(i)  = spieler_score;
  303.         
  304.         save_hisc();
  305.         highscore_show();
  306.     }
  307. }
  308.